<?php defined('SYSPATH') OR die('No direct access allowed.'); ?>
<div class="ui-widget searchBox">
  <div class="ui-widget-header ui-corner-all page-header-text">Select Your Bus</div>
  <div class="filterResultsBox">
  	<input type="text" class="search-text-box search-field noValueIndicator" id="searchBuses" value=""></input>
  	<span style="align:left; width: 200px;"> or </span>
    <select id="pickupcombo" style="align:left;width:auto;height:auto;"></select>  	
  </div>
  	<div class="table-wrapper">
		<table id="busDetails">
			<thead>
				<tr>
					<th class="table-header ocean">Travels</th>
					<th class="table-header ocean">Starting Point</th>
					<th class="table-header ocean">Boarding Time</th>
				</tr>
				<tbody id="busDetailsBody">
				</tbody>
			</thead>
		</table>
	</div>
	<button id="table-clear-button">View All</button>
</div>

<script>
busDetails = <?php echo json_encode($buses); ?>;
function addBus(index, bus) {	
  	$('#busDetails').append("<tr class='ui-widget-content table-row clickable' id='busDetails"+index+"'><td>"+bus.travels+"</td><td> "+bus.bps[0].location+"</td><td> "+bus.bps[0].bpTime+"</td></tr>");
  	$('#busDetails'+index).data("details", bus);
}

//Forwarding to tracker
function changeViewToTrack(selectedBusID, busDetails) {
	selectedBusDetails = busDetails;
	$.ajax({url: 'index.php/welcome/track/'+selectedBusID, success: function(data) {$("#pageContent").html(data)}, dataType: 'text'});
}

$(function() {
  $( "#messageBox" ).dialog( 'close');
  //Populating the Bus table
  $.each(busDetails, addBus);
  
  //Search textbox operations
  var noValueText = "Filter by Travels Name..";
  $("#searchBuses").val(noValueText);
  $("#searchBuses").focus(function(){
  	if($(this).val() == noValueText) {
  		$(this).removeClass("noValueIndicator");
  		$(this).val("");
  		return;
  	}  	
  });
  $("#searchBuses").blur(function(){
  	if($(this).val() == "") {
  		$("#searchBuses").val(noValueText);
  		$(this).val(noValueText).addClass("noValueIndicator");
  	}
  });
  
  
  //Table Operations
  $('#busDetails').tablesorter({sortList: [[0,0], [1,0]]});  
  $('#searchBuses').keyup(function(event) {  
      if (event.keyCode == 27 || $(this).val() == '') {  
        $(this).val('');  
        $('#busDetailsBody tr').show();  
      }  
      else {  
        filter('#busDetailsBody tr', $(this).val());  
      } 
  });
  $('#busDetails tr').click(function () {
  	changeViewToTrack(100, $(this).data("details"));
  });  
  $('#busDetails tr').hover(function () {
    	$(this).toggleClass('ui-state-hover');
  });
  
  $('#table-clear-button').click(function() {
  	filter('#busDetailsBody tr', '');
  });


  ///Pickup Point combo box...
  var combo = $( "#pickupcombo" ).combobox({
  	selected: function(event, ui) {  	  	
  	}		
  });
  
});
</script>
